﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SalesCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 修改退货通知单所有信息命令处理方法
    /// </summary>
    public class UdpreturnmainCommandHandler:IRequestHandler<UdpreturnmainCommand,APIResult<int>>
    {
        private readonly IBaseRepository<return_notice_main> rRepo;
        private readonly IBaseRepository<OrderMaterials> oRepo;

        public UdpreturnmainCommandHandler(IBaseRepository<return_notice_main> rRepo, IBaseRepository<OrderMaterials> oRepo)
        {
            this.rRepo = rRepo;
            this.oRepo = oRepo;
        }

        public Task<APIResult<int>> Handle(UdpreturnmainCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = rRepo.GetAll().Result.Where(x => x.notice_id == request.notice_id).FirstOrDefault();
            list.notice_id = request.notice_id;
            list.document_code = request.document_code;
            list.document_date = request.document_date;
            list.business_type_id = request.business_type_id;
            list.warehouse_id = request.warehouse_id;
            list.operators = request.operators;
            list.customer_name = request.customer_name;
            list.document_status = request.document_status;
            list.auditor = request.auditor;
            list.audit_date = request.audit_date;
            list.business_status_id = request.business_status_id;
            list.return_reason_code = request.return_reason_code;
            list.handling_method_code = request.handling_method_code;
            list.remark = request.remark;
            var r = rRepo.Update(list).Result;
            if (r > 0)
            {             
                //OrderMaterials or = new OrderMaterials();
                var or = oRepo.GetAll().Result.Where(x => x.SaleCode == request.document_code).FirstOrDefault();
                foreach (var item in request.OrderMaterials)
                {
                    or.MaterialsCode = item.MaterialsCode;
                    or.MaterialsName = item.MaterialsName;
                    or.ModelSpecification = item.ModelSpecification;
                    or.Calulate = item.Calulate;
                    or.CalulateNumber = item.CalulateNumber;
                    or.Price = item.Price;
                    or.TotalPrice = item.TotalPrice;
                    or.QualityRequir = item.QualityRequir;
                    or.PackRequir = item.PackRequir;
                    or.Batch = item.Batch;
                    or.OutBoundNumber = item.OutBoundNumber;
                }
                var g = oRepo.Update(or).Result;
                if (g > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = "更改成功";
                    result.data = g;
                    return Task.FromResult(result);
                }


            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = "更改失败";
                return Task.FromResult(result);
            }
            return Task.FromResult(result);
        }

        

    }


}
